package com.project.aiqicha;

import cn.hutool.core.net.URLEncoder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.frame.common.model.BaseResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.HashMap;
import java.util.Map;

/**
 * desc
 *
 * @author cjq
 * @date 2023-08-29
 */
@Configuration
@EnableScheduling
@Slf4j
public class AiqichaTask2 {

    static int idx = 0;

    @Autowired
    AiqichaService aiqichaService;


//    @Scheduled(fixedDelay = 5000) // 每隔n秒执行一次
    public void run(){
        long start = System.currentTimeMillis();
        try{
            advanceFilterAjax("220000","吉林省");
        }catch(Exception e){
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info("diff="+(end-start));
    }



    private void advanceFilterAjax(String code, String name) {
        for(int i=1;i<=1000;i++){
            Map<String,String> headerMap = new HashMap<>();
            String cookie = aiqichaService.getCookie();
            headerMap.put("Cookie",cookie);
            headerMap.put("Referer","https://aiqicha.baidu.com/s?city=%E5%90%89%E6%9E%97%E7%9C%81&f={%22provinceCode%22:[%22220000%22]}");
            String url = "https://aiqicha.baidu.com/s/advanceFilterAjax?t=&p="+(i+1)+"&s=10&o=0&f=%7B%22provinceCode%22:[%22220000%22]%7D";
//            String params = "t=&p="+i+"&s=10&o=0&f={\"provinceCode\":[\""+code+"\"]}";
//            url = url+"?"+ URLEncoder.createDefault().encode(params, CharsetUtil.CHARSET_UTF_8);
            HttpResponse res = HttpUtil.createGet(url).headerMap(headerMap,true).execute();
            idx++;
            if(res.isOk()){
                log.info("b.ok.count="+idx);
                String body = res.body();
                JSONObject bodyObj = JSONObject.parseObject(body);
                JSONObject dataObj = bodyObj.getJSONObject("data");
                JSONArray resultList = dataObj.getJSONArray("resultList");
                openDetail(resultList);
            }else{
                log.info("b.fail.count="+idx);
            }
        }

    }

    private void openDetail(JSONArray detailList) {
        for (int i = 0; i < detailList.size(); i++) {
            idx++;
            JSONObject obj = detailList.getJSONObject(i);
            String pid = obj.getString("pid");
            String entName = obj.getString("entName");
            String regNo = obj.getString("regNo");
            BaseResp resp = aiqichaService.search(entName,"");
            if(resp.isSuccess()==false){
                log.info("entName="+entName+","+JSON.toJSONString(resp));
            }else{
                log.info("entName="+entName+",t.ok.count="+idx);
            }
        }
    }

}
